package com.linkage.seapeak.view;

import android.Manifest;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.OvershootInterpolator;
import android.widget.PopupWindow;

import com.linkage.jz.bankruptcy.R;
import com.linkage.jz.bankruptcy.activity.Main.DateCalcActivity;
import com.linkage.jz.bankruptcy.activity.Main.PenaltyCalcActivity;
import com.linkage.jz.bankruptcy.activity.Main.RateCalcActivity;
import com.linkage.jz.bankruptcy.activity.Project.OtherUserDetailActivity;
import com.linkage.jz.bankruptcy.activity.Project.ZQUser.ZQUserDetailActivity;
import com.linkage.jz.bankruptcy.widget.PublicNoticeDialog;
import com.linkage.seapeak.activity.chat.ChatDetailActivity;
import com.linkage.seapeak.utils.CommonUtils;
import com.linkage.seapeak.utils.ToastUtil;

import static com.linkage.jz.bankruptcy.fragment.Main.CarryOutListFrgment.REQUEST_CALL_PERMISSION;
import static com.linkage.seapeak.base.BaseActivity.REQUEST_SMS_PERMISSION;

/**
 * 快速应用弹框
 */
public class QuickUserPopView {

    private static String mUserid;
    private static String mProid;
    private static String mUsername;
    private static String mPhone;
    private static Activity mContext;

    private static Boolean flag = false;
    private static PublicNoticeDialog noticeDialog;
    public static PopupWindow show(final Activity context,String userid,String proid,String usename,String phone) {

        mUserid = userid;
        mProid =proid;
        mUsername = usename;
        mPhone = phone;
        mContext = context;
        View contentView;
        LayoutInflater mLayoutInflater = LayoutInflater.from(context);
        contentView = mLayoutInflater.inflate(R.layout.view_pop_quick_user_tool,
                null);
        final PopupWindow pop = new PopupWindow(contentView,
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        final View phoneView = contentView.findViewById(R.id.phone_view);
        final View smsView = contentView.findViewById(R.id.sms_view);
        final View pop_click = contentView.findViewById(R.id.pop_click);

        contentView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    return true;
                }
                return false;
            }
        });


        // 产生背景变暗效果
        WindowManager.LayoutParams lp = context.getWindow()
                .getAttributes();
        lp.alpha = 0.4f;
        context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        context.getWindow().setAttributes(lp);
        pop.setTouchable(true);
        pop.setFocusable(true);
        pop.setBackgroundDrawable(new BitmapDrawable());
        pop.setOutsideTouchable(true);
        pop.showAtLocation(contentView, Gravity.BOTTOM, 0, 0);
        pop.update();
        pop.setOnDismissListener(new PopupWindow.OnDismissListener() {

            // 在dismiss中恢复透明度
            public void onDismiss() {
                WindowManager.LayoutParams lp = context.getWindow()
                        .getAttributes();
                lp.alpha = 1f;
                context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                context.getWindow().setAttributes(lp);
            }
        });

        contentView.post(new Runnable() {
            @Override
            public void run() {
                ValueAnimator animator = ValueAnimator.ofFloat(0f, 1f);
                animator.setDuration(400);
                animator.setInterpolator(new OvershootInterpolator());
                animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        float value = (float) animation.getAnimatedValue() * CommonUtils.dp2px(context, 80);
                        phoneView.setAlpha((float) animation.getAnimatedValue());
                        smsView.setAlpha((float) animation.getAnimatedValue());
                        phoneView.setTranslationX(-value);
                        smsView.setTranslationX(-value * 0.8f);
                        smsView.setTranslationY(-value * 0.8f);
//                        interestView.setTranslationY(-value);
                        pop_click.setRotation(45 + (float) animation.getAnimatedValue() * 45);
                    }
                });
                animator.start();
            }
        });

        phoneView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(phone!=null && !"".equalsIgnoreCase(phone)) {
//                                                    noticeDialog.dismiss();
//                                                    flag =false;
                    final String phone_tmp = phone;
//                                                    SetPhone(phone);
                    noticeDialog = new PublicNoticeDialog(mContext, new View.OnClickListener(){

                        @Override
                        public void onClick(View v) {
                            flag = false;
                            switch (v.getId()) {
                                case R.id.ll_close:
                                    noticeDialog.dismiss();
                                    break;
                                case R.id.btn_chancel_notice:

                                    if(checkReadPermission(Manifest.permission.CALL_PHONE,REQUEST_SMS_PERMISSION)){

                                        try {
//                    Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(phone));

                                            Intent intent = new Intent(Intent.ACTION_SENDTO);
                                            Uri data = Uri.parse("smsto:" + phone_tmp);
                                            intent.setData(data);
                                            mContext.startActivity(intent);

//                                                Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:"+phoneNumber));
//                                                intent.putExtra("sms_body", message);
//                                                startActivity(intent);

                                        }catch (Exception ex){
                                            ex.printStackTrace();
                                        }
                                    }
                                    noticeDialog.dismiss();
                                    break;

                                case R.id.btn_save_notice:
                                    if(!"".equalsIgnoreCase(phone)) {
                                        if(checkReadPermission(Manifest.permission.CALL_PHONE,REQUEST_CALL_PERMISSION)){

                                            try {
//                    Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(phone));

                                                Intent intent = new Intent(Intent.ACTION_CALL);
                                                Uri data = Uri.parse("tel:" + phone);
                                                intent.setData(data);
                                                mContext.startActivity(intent);
                                            }catch (Exception ex){
                                                ex.printStackTrace();
                                            }
                                        }
                                    }
                                    noticeDialog.dismiss();

                                    break;

                            }
                            noticeDialog.dismiss();
                        }
                    }, 2,phone, phone,"发短信","拨打电话");
                    noticeDialog.show();
                }else{
                    ToastUtil.getInstance(mContext).showShortToast("无联系人号码");
                }

                ValueAnimator animator = ValueAnimator.ofFloat(1f, 0f);
                animator.setDuration(350);
                animator.setInterpolator(new DecelerateInterpolator());
                animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {



                        float value = (float) animation.getAnimatedValue() * CommonUtils.dp2px(context, 80);
                        phoneView.setAlpha((float) animation.getAnimatedValue());
                        smsView.setAlpha((float) animation.getAnimatedValue());
//                        interestView.setAlpha((float) animation.getAnimatedValue());
                        phoneView.setTranslationX(-value);
                        smsView.setTranslationX(-value * 0.8f);
                        smsView.setTranslationY(-value * 0.8f);

//                        interestView.setTranslationY(-value);
                        pop_click.setRotation(45 + (float) animation.getAnimatedValue() * 45);
                        if ((float) animation.getAnimatedValue() < 0.1f) {
                            /**
                             * todo
                             *
                             * 电话事件
                             *
                             *
                             */
                            if(flag)
                            {
                                return;
                            }
                            flag = true;

//                            noticeDialog = new PublicNoticeDialog(context, new View.OnClickListener() {
//                                @Override
//                                public void onClick(View view) {
//
//                                    if (view.getId() == R.id.btn_chancel_notice) {//暂存
//
//                                        noticeDialog.dismiss();
//                                        flag =false;
//                                    } else if (view.getId() == R.id.ll_close) {
//                                        noticeDialog.dismiss();
//                                        flag =false;
//                                    } else {//生成
//                                        if(checkReadPermission(Manifest.permission.CALL_PHONE,REQUEST_CALL_PERMISSION)){
//
//                                            try {
//                    Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(phone));

//                                                if(phone!=null && !"".equalsIgnoreCase(phone)) {
////                                                    noticeDialog.dismiss();
////                                                    flag =false;
//                                                    final String phone_tmp = phone;
////                                                    SetPhone(phone);
//                                                    noticeDialog = new PublicNoticeDialog(mContext, new View.OnClickListener(){
//
//                                                        @Override
//                                                        public void onClick(View v) {
//                                                            flag = false;
//                                                            switch (v.getId()) {
//                                                                case R.id.ll_close:
//                                                                    noticeDialog.dismiss();
//                                                                    break;
//                                                                case R.id.btn_chancel_notice:
//
//                                                                    if(checkReadPermission(Manifest.permission.CALL_PHONE,REQUEST_SMS_PERMISSION)){
//
//                                                                        try {
////                    Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(phone));
//
//                                                                            Intent intent = new Intent(Intent.ACTION_SENDTO);
//                                                                            Uri data = Uri.parse("smsto:" + phone_tmp);
//                                                                            intent.setData(data);
//                                                                            mContext.startActivity(intent);
//
////                                                Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:"+phoneNumber));
////                                                intent.putExtra("sms_body", message);
////                                                startActivity(intent);
//
//                                                                        }catch (Exception ex){
//                                                                            ex.printStackTrace();
//                                                                        }
//                                                                    }
//                                                                    noticeDialog.dismiss();
//                                                                    break;
//
//                                                                case R.id.btn_save_notice:
//                                                                    if(!"".equalsIgnoreCase(phone)) {
//                                                                        if(checkReadPermission(Manifest.permission.CALL_PHONE,REQUEST_CALL_PERMISSION)){
//
//                                                                            try {
////                    Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(phone));
//
//                                                                                Intent intent = new Intent(Intent.ACTION_CALL);
//                                                                                Uri data = Uri.parse("tel:" + phone);
//                                                                                intent.setData(data);
//                                                                                mContext.startActivity(intent);
//                                                                            }catch (Exception ex){
//                                                                                ex.printStackTrace();
//                                                                            }
//                                                                        }
//                                                                    }
//                                                                    noticeDialog.dismiss();
//
//                                                                    break;
//
//                                                            }
//                                                            noticeDialog.dismiss();
//                                                        }
//                                                    }, 2,phone, phone,"发短信","拨打电话");
//                                                    noticeDialog.show();
//                                                }else{
//                                                    ToastUtil.getInstance(mContext).showShortToast("无联系人号码");
//                                                }
//                                            }catch (Exception ex){
//                                                ex.printStackTrace();
//                                            }
//                                        }
//                                        noticeDialog.dismiss();
                                        flag =false;
//                                    }
//                    saveData(sendStatus);
//                                    noticeDialog.dismiss();

//                                }
//                            }, 2, "提示", phone, "取消", "呼叫");

//                            if(flag) {
                                pop.dismiss();
//                                return;
//                            }else{
//                                flag =true;
//                                noticeDialog.show();
//
//                            }



                            }


                            pop.dismiss();

                    }
                });
                animator.start();
            }
        });

        smsView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ValueAnimator animator = ValueAnimator.ofFloat(1f, 0f);
                animator.setDuration(350);
                animator.setInterpolator(new DecelerateInterpolator());
                ChatDetailActivity.start(context,mUserid,
                        mUsername, mProid);
                animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        float value = (float) animation.getAnimatedValue() * CommonUtils.dp2px(context, 80);
                        phoneView.setAlpha((float) animation.getAnimatedValue());
                        smsView.setAlpha((float) animation.getAnimatedValue());
//                        interestView.setAlpha((float) animation.getAnimatedValue());
                        phoneView.setTranslationX(-value);
                        smsView.setTranslationX(-value * 0.8f);
                        smsView.setTranslationY(-value * 0.8f);
//                        interestView.setTranslationY(-value);
                        pop_click.setRotation(45 + (float) animation.getAnimatedValue() * 45);
                        if ((float) animation.getAnimatedValue() < 0.1f) {
                            /**
                             * todo
                             *
                             * 站内信事件
                             *
                             *
                             */


                            pop.dismiss();
                        }
                    }
                });
                animator.start();
            }
        });

        pop_click.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ValueAnimator animator = ValueAnimator.ofFloat(1f, 0f);
                animator.setDuration(350);
                animator.setInterpolator(new AccelerateInterpolator());
                animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        float value = (float) animation.getAnimatedValue() * CommonUtils.dp2px(context, 80);
                        phoneView.setAlpha((float) animation.getAnimatedValue());
                        smsView.setAlpha((float) animation.getAnimatedValue());
//                        interestView.setAlpha((float) animation.getAnimatedValue());
                        phoneView.setTranslationX(-value);
                        smsView.setTranslationX(-value * 0.8f);
                        smsView.setTranslationY(-value * 0.8f);
//                        interestView.setTranslationY(-value);
                        pop_click.setRotation(45 + (float) animation.getAnimatedValue() * 45);
                        if ((float) animation.getAnimatedValue() < 0.1f)
                            pop.dismiss();
                    }
                });
                animator.start();
            }
        });
        return pop;
    }

    private static boolean checkReadPermission(String string_permission,int request_code) {
        boolean flag = false;
        if (ContextCompat.checkSelfPermission(mContext, string_permission) == PackageManager.PERMISSION_GRANTED) {//已有权限
            flag = true;
        } else {//申请权限
            ActivityCompat.requestPermissions(mContext, new String[]{string_permission}, request_code);
        }
        return flag;
    }
}
